Day 4: Shiny apps

A short description of the post.

Ben Fanson https://bfanson.github.io/2024DADAworkshop/ (Arthur Rylah Institute)https://www.ari.vic.gov.au/
2024-09-01

General thoughts

  1. For Shiny Apps, the code looks foreign and a bit are you serious after all this time I spent learning R coding

  2. Once you get the basic structure, you can easily get started and then it is “Trial and Error”

Day Objectives

  1. Understand the core structure of Shiny apps: UI, Server, Reactivity

  2. Learn how to create and run an App locally

  3. Learn how to deploy your App on ARI’s shiny account

Think about how shiny Apps might be useful

Visualising data for clients

Ibis Tracker - Nev masterpiece

https://arisci.shinyapps.io/ibisTracker/

Data sharing - upload

Model simulations

Reminder on a few R fundamentals

LISTS

Creating a shiny app using Rstudio

Show code
#install.packages("shiny")

You can find a variety of cheatsheets at https://posit.co/resources/cheatsheets/

Show code
  knitr::include_graphics("shiny.pdf")










s

Show code
# library(shiny) 
# ui <- fluidPage(   "Hello, world!" ) 
# server <- function(input, output, session) { } 
# shinyApp(ui, server)

steps

  1. add UI controls
  1. add “behaviour” Shiny uses reactive programming to make apps interactive.

you put R code in renderFunction

Shiny Templates

extras at < https://github.com/nanxstats/awesome-shiny-extensions>

Exercise

Let’s create replicate Examples https://shiny.posit.co/r/gallery/

Walk through of creating App for our dataset

App design

Let’s build something

Jumping back to workflow for Apps

This will become pretty useful for collaborations and your own self-learning. Also, we now have a ARI shiny account and consistency allows for increased sharing and understanding.

Useful interactive packages for shiny

can check https://r-graph-gallery.com/interactive-charts.html

leaflet

plotly

Show code
  library(ggplot2)
  library(plotly)
  f <- ggplot( cars, aes(speed, dist)  ) + geom_point() + geom_smooth()
   plotly::ggplotly(f)

ggiraph

Show code
library(ggiraph)
library(tidyverse)
library(patchwork)

mtcars_db <- rownames_to_column(mtcars, var = "carname")

# First plot: Scatter plot
fig_pt <- ggplot(
  data = mtcars_db,
  mapping = aes(
    x = disp, y = qsec,
    tooltip = carname, data_id = carname
  )
) +
  geom_point_interactive(
    size = 3, hover_nearest = TRUE
  ) +
  labs(
    title = "Displacement vs Quarter Mile",
    x = "Displacement", y = "Quarter Mile"
  ) +
  theme_bw()

# Second plot: Bar plot
fig_bar <- ggplot(
  data = mtcars_db,
  mapping = aes(
    x = reorder(carname, mpg), y = mpg,
    tooltip = paste("Car:", carname, "<br>MPG:", mpg),
    data_id = carname
  )
) +
  geom_col_interactive(fill = "skyblue") +
  coord_flip() +
  labs(
    title = "Miles per Gallon by Car",
    x = "Car", y = "Miles per Gallon"
  ) +
  theme_bw()

# Combine the plots using patchwork
 combined_plot <- fig_pt + fig_bar + plot_layout(ncol = 2) 

# Combine the plots using cowplot
# combined_plot <- cowplot::plot_grid(fig_pt, fig_bar, ncol=2) 

# Create a single interactive plot with both subplots
interactive_plot <- girafe(ggobj = combined_plot)

# Set options for the interactive plot
girafe_options(
  interactive_plot,
  opts_hover(css = "fill:cyan;stroke:black;cursor:pointer;"),
  opts_selection(type = "single", css = "fill:red;stroke:black;")
)


How a shiny App code might look of the above…


Deploy your App

ARI shiny account

rsconnect package

Show code
install.packages('rsconnect')
install.packages('rsconnect')
rsconnect::setAccountInfo(name="<ACCOUNT>", token="<TOKEN>", secret="<SECRET>")

Shot bit on layouts

https://shiny.posit.co/r/layouts/

Beyond showing results

Uploading files

Saving user inputs

Downloading

Additional resources

For useful resources: https://shiny.posit.co/r/articles/ For useful examples: https://shiny.posit.co/r/articles/

see https://mastering-shiny.org/